//
//  SDSettingsViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/7/6.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDSettingsViewController.h"
#import "SDResetPswdViewController.h"
#import "SDSettingTableViewCell.h"
#import "SDLoginManager.h"
#import "SDAppConfigManager.h"
#import "SDMessageListViewController.h"
#import "SDAboutViewController.h"
#import "SDWindowManager.h"
#import "SDTabBarController.h"
#import "SDGuideLoginViewController.h"
#import "SDFeedbackViewController.h"

#define kSettingCellHeight      46.f

@interface SDSettingsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) UIButton *photoButton;
@property(nonatomic,strong) UIButton *takePhotoButton;
@property(nonatomic,strong) UIButton *paientButton;

@end

@implementation SDSettingsViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubviews];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (BOOL)navigationBarHidden
{
    return NO;
}

-(void)initSubviews
{
    BOOL naviHidden = [self navigationBarHidden];
    if (!naviHidden && self.navBar != nil)
    {
        [self.navBar setTitle:@"设置"];
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _tableView = [[UITableView alloc] initWithFrame:autoRectTableViewHaveBar(YES)  style:UITableViewStyleGrouped];
    _tableView.height -= MainTabBar_H;
    _tableView.backgroundColor = [UIColor colorWithRGB:0xf9fafc];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginDidSuccessNotification:) name:kLoginMgrDidSucceedNotificaion object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginDidLogOutNotification:) name:kLoginServiceDidLogOutNotification object:nil];
}

-(UIView*)createHeaderViewWithTitle:(NSString*)title
{
    UIView *view =  [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 12)];
    view.backgroundColor = [UIColor redColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 12)];
    titleLabel.font = [UIFont systemFontOfSize:12];
    titleLabel.textColor = [UIColor colorWithRGB:0xb4bec8];
    [view addSubview:titleLabel];
    titleLabel.text = title;
    [titleLabel sizeToFit];
    titleLabel.center = view.center;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    return kSettingTableViewCellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (0 == section)
    {
        return 60.0f;
    }
    
    return 40.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            UIView *view =  [[UIView alloc]initWithFrame:CGRectMake(0, 40, kScreenWidth, 12)];
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:view.bounds];
            titleLabel.font = [UIFont systemFontOfSize:12];
            titleLabel.textColor = [UIColor colorWithRGB:0xb4bec8];
            [view addSubview:titleLabel];
            titleLabel.text = @"通用设置";
            [titleLabel sizeToFit];
            titleLabel.center = view.center;
            return view;
        }
            break;
        case 1:
        {
            UIView *view =  [[UIView alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, 12)];
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:view.bounds];
            titleLabel.font = [UIFont systemFontOfSize:12];
            titleLabel.textColor = [UIColor colorWithRGB:0xb4bec8];
            [view addSubview:titleLabel];
            titleLabel.text = @"系统信息";
            [titleLabel sizeToFit];
            titleLabel.center = view.center;
            return view;
        }
            break;
        case 2:
        {
            UIView *view =  [[UIView alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, 12)];
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:view.bounds];
            titleLabel.font = [UIFont systemFontOfSize:12];
            titleLabel.textColor = [UIColor colorWithRGB:0xb4bec8];
            [view addSubview:titleLabel];
            titleLabel.text = @"账户设置";
            [titleLabel sizeToFit];
            titleLabel.center = view.center;
            return view;
        }
            break;
        default:
            break;
    }
    return nil;
}

#pragma mark -
#pragma mark UITableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section)
    {
        case 0:
            return 1;
        case 1:
            return 4;
        case 2:
            return 2;
        default:
            return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *curCell = nil;
        switch (indexPath.section)
    {
        case 0:
        {
            SDSettingTableViewCell* cell = nil;
            static NSString* identifier = @"bootCell";
            cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if( cell == nil )
            {
                cell = [[SDSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                
                UIImage* normalImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_picture.png");
                UIImage* selectImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_picture_h.png");
                _photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [_photoButton addTarget:self action:@selector(onBootButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [_photoButton setImage:normalImg forState:UIControlStateNormal];
                [_photoButton setImage:selectImg forState:UIControlStateSelected];
                [_photoButton setTitle:@"照片" forState:UIControlStateNormal];
                 _photoButton.titleLabel.font = [UIFont systemFontOfSize:12];
                [_photoButton setTitleColor:[UIColor colorWithRGB:0xb4bec8] forState:UIControlStateNormal];
                [_photoButton setTitleColor:[UIColor colorWithRGB:0x24d9c6] forState:UIControlStateSelected];
                [_photoButton setFrame:CGRectMake(0, 0, 60, 30)];
                _photoButton.left = cell.titleLabel.right + 30;
                _photoButton.top = (kSettingTableViewCellHeight - _photoButton.height)/2;
                [_takePhotoButton sizeToFit];
                [cell.contentView addSubview:_photoButton];
                
                normalImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_camera.png");
                selectImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_camera_h.png");
                _takePhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [_takePhotoButton addTarget:self action:@selector(onBootButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [_takePhotoButton setImage:normalImg forState:UIControlStateNormal];
                [_takePhotoButton setImage:selectImg forState:UIControlStateSelected];
                [_takePhotoButton setTitle:@"拍照" forState:UIControlStateNormal];
                _takePhotoButton.titleLabel.font = [UIFont systemFontOfSize:12];
                [_takePhotoButton setTitleColor:[UIColor colorWithRGB:0xb4bec8] forState:UIControlStateNormal];
                [_takePhotoButton setTitleColor:[UIColor colorWithRGB:0x24d9c6] forState:UIControlStateSelected];
                [_takePhotoButton setFrame:CGRectMake(0, 0, 60, 30)];
                _takePhotoButton.left = _photoButton.right + 10;
                _takePhotoButton.top = (kSettingTableViewCellHeight - _takePhotoButton.height)/2;
                [cell.contentView addSubview:_takePhotoButton];
                
                normalImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_patient.png");
                selectImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_patient_h.png");
                _paientButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [_paientButton addTarget:self action:@selector(onBootButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [_paientButton setImage:normalImg forState:UIControlStateNormal];
                [_paientButton setImage:selectImg forState:UIControlStateSelected];
                [_paientButton setTitle:@"患者" forState:UIControlStateNormal];
                _paientButton.titleLabel.font = [UIFont systemFontOfSize:12];
                [_paientButton setTitleColor:[UIColor colorWithRGB:0xb4bec8] forState:UIControlStateNormal];
                [_paientButton setTitleColor:[UIColor colorWithRGB:0x24d9c6] forState:UIControlStateSelected];
                [_paientButton setFrame:CGRectMake(0, 0, 60, 30)];
                _paientButton.left = _takePhotoButton.right + 10;
                _paientButton.top = (kSettingTableViewCellHeight - _paientButton.height)/2;
                [cell.contentView addSubview:_paientButton];

            }
            [self setCurrentBootTabIndex:[[SDAppConfigManager sharedInstance] getBootTabSelectedIndex]];
            [cell setCellData:@"启动画面"];
            curCell = cell;
            break;
        }
        case 1:
        {
            if(0==indexPath.row)
            {
                SDSettingTableViewCell* cell = nil;
                static NSString* identifier = @"sysNotificationCell";
                cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if( cell == nil )
                {
                    cell = [[SDSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                }
                cell.arrowLabel.text = @">";
                [cell setCellData:@"系统通知"];
                cell.bottomLineView.left = kTagLeftPadding;
                cell.bottomLineView.width = kScreenWidth - 2*kTagLeftPadding;
                curCell = cell;
            }
            else if(1 == indexPath.row)
            {
                SDSettingTableViewCell* cell = nil;
                static NSString* identifier = @"appVersionCell";
                cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if( cell == nil )
                {
                    cell = [[SDSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                }
                cell.arrowLabel.text = [SDAppUtils getAppBuildVersion];
                [cell setCellData:@"app版本号"];
                cell.topLineView.hidden = YES;
                cell.bottomLineView.left = kTagLeftPadding;
                cell.bottomLineView.width = kScreenWidth - 2*kTagLeftPadding;
                curCell  = cell;
            }
           else  if(2 == indexPath.row)
            {
                SDSettingTableViewCell* cell = nil;
                static NSString* identifier = @"aboutCell";
                cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if( cell == nil )
                {
                    cell = [[SDSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                }
                cell.arrowLabel.text = @">";
                [cell setCellData:@"关于"];
                cell.topLineView.hidden = YES;
                cell.bottomLineView.left = kTagLeftPadding;
                cell.bottomLineView.width = kScreenWidth - 2*kTagLeftPadding;
                curCell = cell;
            }
           else  if(3 == indexPath.row)
           {
               SDSettingTableViewCell* cell = nil;
               static NSString* identifier = @"feedbackCell";
               cell = [tableView dequeueReusableCellWithIdentifier:identifier];
               if( cell == nil )
               {
                   cell = [[SDSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
               }
               cell.arrowLabel.text = @">";
               [cell setCellData:@"意见反馈"];
               cell.topLineView.hidden = YES;
               curCell = cell;
           }

            break;
            
        }
        case 2:
        {
            if(0==indexPath.row)
            {
                SDSettingTableViewCell* cell = nil;
                static NSString* identifier = @"modifyPswdCell";
                cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if( cell == nil )
                {
                    cell = [[SDSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                }
                cell.arrowLabel.text = @">";
                [cell setCellData:@"修改密码"];
                cell.bottomLineView.left = kTagLeftPadding;
                cell.bottomLineView.width = kScreenWidth - 2*kTagLeftPadding;
                curCell = cell;
            }
            else if(1==indexPath.row)
            {
                SDSettingTableViewCell* cell = nil;
                static NSString* identifier = @"logoutCell";
                cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if( cell == nil )
                {
                    cell = [[SDSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                }
                cell.topLineView.hidden = YES;
                BOOL isLogin = [[SDLoginManager sharedInstance] isLogin];
                if (isLogin) {
                    cell.arrowLabel.text = [SDAccountInfoManager sharedInstance].nickName;
                    cell.arrowLabel.textColor = [UIColor colorWithRGB:0x12d2c4];
                    [cell setCellData:@"退出登录"];
                } else {
                    [cell setCellData:@"登录"];
                }
                
                curCell = cell;
            }
            break;
        }
        default:
            break;
    }
    return curCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    switch (section) {
        case 1:
            if (row == 0) {
                if (row == 0) {
                    SDMessageListViewController* messageListVC = [[SDMessageListViewController alloc] init];
                    [self.navigationController pushViewController:messageListVC animated:YES];
                }
            } else if(row == 1) {
                
            } else if(row == 2) {
                SDAboutViewController* aboutVC = [[SDAboutViewController alloc] init];
                [self.navigationController pushViewController:aboutVC animated:YES];
            } else if(row == 3) {
                SDFeedbackViewController* aboutVC = [[SDFeedbackViewController alloc] init];
                [self.navigationController pushViewController:aboutVC animated:YES];
            }
            break;
        case 2:
            if (row == 0) {
                SDResetPswdViewController* resetPswdVC = [[SDResetPswdViewController alloc] init];
                [self.navigationController pushViewController:resetPswdVC animated:YES];
            } else if(row == 1) {
                
                BOOL isLogin = [[SDLoginManager sharedInstance] isLogin];
                if (isLogin) {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                        message:@"确认退出登录吗"
                                                                       delegate:nil
                                                              cancelButtonTitle:@"取消"
                                                              otherButtonTitles:@"确认", nil];
                    
                    alertView.clickedButtonBlock = ^(UIAlertView *alertView, NSInteger buttonIndex) {
                        
                        if (buttonIndex == 1) {
                            [[SDLoginManager sharedInstance] logOut];
                            
                            SDGuideLoginViewController* loginViewCtrl = [[SDGuideLoginViewController alloc] init];
                            loginViewCtrl.skipBtnHidden = YES;
                            [[SDWindowManager defaultManager] showUserWindow:loginViewCtrl];
                            SDTabBarController* tabBarCtrl = [[SDWindowManager defaultManager] getTabBarController];
                            [tabBarCtrl selectBootTabIndex];
                           // [self.tableView reloadData];
                        }
                    };
                    
                    [alertView show];
                } else {
                    [self showLoginViewController];
                }
            }
            break;
        default:
            break;
    }
}

#pragma mark- UI Event
-(void)onBootButtonClicked:(id)sender
{
    int tabIndex = -1;
    if (_photoButton == sender) {
        tabIndex = 0;
    } else  if (_takePhotoButton == sender) {
        tabIndex = 1;
    }  if (_paientButton == sender) {
        tabIndex = 2;
    }
    [[SDAppConfigManager sharedInstance] setBootTabIndex:tabIndex];
    [self setCurrentBootTabIndex:tabIndex];
}

-(void)setCurrentBootTabIndex:(int)selectedIndex
{
    if (selectedIndex == 0) {
        _photoButton.selected = YES;
        _takePhotoButton.selected = NO;
        _paientButton.selected = NO;
    } else if(selectedIndex == 1){
        _takePhotoButton.selected = YES;
        _photoButton.selected = NO;
        _paientButton.selected = NO;
    } else if(selectedIndex == 2){
        _paientButton.selected = YES;
        _photoButton.selected = NO;
        _takePhotoButton.selected = NO;
    }
}

#pragma mark -
#pragma mark -  Login Notifications
- (void)loginDidSuccessNotification:(NSNotification *)not
{
    [self.tableView reloadData];
}

- (void)loginDidLogOutNotification:(NSNotification *)not
{
    [self.tableView reloadData];
}
@end
